package edu.zzuli.test.controller;

import edu.zzuli.common.MailUtil;
import edu.zzuli.common.UUidUtil;
import edu.zzuli.test.pojo.User;
import edu.zzuli.test.service.ThumbnailService;
import edu.zzuli.test.service.UploadService;
import edu.zzuli.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Chen-D.W on 2017/12/23.
 */
@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ThumbnailService thumbnailService;

    @RequestMapping("/index1")
    public String index1(HttpServletRequest request,Model model){
        List<User> userList=userService.getUserList();
        model.addAttribute("userList",userList);
        return "index1";
    }

    @RequestMapping("/index2")
    public String index2(HttpServletRequest request){
        return "index2";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, Model model){
        return "login";
    }

    @RequestMapping(value = "/mailServlet",method = RequestMethod.POST)
    public String mailServilet(HttpServletRequest request,HttpServletResponse response ,User user,Model model){
        user.setStatus(Boolean.parseBoolean("0"));
        String code=UUidUtil.getUUid()+UUidUtil.getUUid();
        user.setCode(code);
        userService.createUser(user);
        try {
            new Thread(new MailUtil(user.getMail(),code)).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("userInf",user);
        model.addAttribute("text","恭喜你注册成功了，请在邮箱中激活账号");
        return "message";
    }

    @RequestMapping("/upload")
    public String upload(){
        return "upImage";
    }

}
